package com.hairline.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.hairline.bean.*;
import com.hairline.bean.bo.*;
import com.hairline.bean.vo.GrouponListRecordVo;
import com.hairline.bean.vo.ListVo;
import com.hairline.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.System;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SpreadServicesImpl implements SpreadServices {

    @Autowired
    AdMapper adMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CouponUserMapper couponUserMapper;
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    GrouponRulesMapper grouponRulesMapper;
    @Autowired
    GrouponMapper grouponMapper;

    //    ---------------------Ad相关-------------------------------
    @Override
    public ListVo getAdListPages(AdListBo adListBo) {
        AdExample adExample = new AdExample();

        PageHelper.startPage(adListBo.getPage(), adListBo.getLimit());
        AdExample.Criteria criteria = adExample.createCriteria();

        if (!StringUtil.isEmpty(adListBo.getName())) {
            String s = "%" + adListBo.getName() + "%";
            criteria.andNameLike(s);
        }

        if (!StringUtil.isEmpty(adListBo.getContent())) {
            criteria.andContentLike("%" + adListBo.getContent() + "%");
        }

        adExample.setOrderByClause(adListBo.getSort() + " " + adListBo.getOrder());


        List<Ad> ads = adMapper.selectByExample(adExample);
        PageInfo<Ad> adPageInfo = new PageInfo<>(ads);
        long total = adPageInfo.getTotal();


        ListVo vo = new ListVo();
        vo.setTotal(total);
        vo.setItems(ads);
        return vo;
    }

    @Override
    public Ad creatAd(Ad creat) {

        int insert = adMapper.insert(creat);

        Ad ad = adMapper.selectByPrimaryKey(creat.getId());

        return ad;
    }

    @Override
    public Ad updateAd(Ad update) {

        int i = adMapper.updateByPrimaryKeySelective(update);

        return update;
    }

    @Override
    public void delete(Ad update) {
        Integer id = update.getId();
        int i = adMapper.deleteByPrimaryKey(id);

    }
    //    ---------------------Coupon相关-------------------------------

    @Override
    public ListVo getCouponList(CouponListBo coupon) {
        CouponExample couponExample = new CouponExample();
        PageHelper.startPage(coupon.getPage(), coupon.getLimit());

        CouponExample.Criteria criteria = couponExample.createCriteria();

        if (!StringUtil.isEmpty(coupon.getName())) {
            criteria.andNameLike("%" + coupon.getName() + "%");
        }

        if (coupon.getType() != null) {
            criteria.andTypeEqualTo(coupon.getType().shortValue());
        }
        if (coupon.getStatus() != null) {
            criteria.andStatusEqualTo(coupon.getStatus().shortValue());
        }

        couponExample.setOrderByClause(coupon.getSort() + " " + coupon.getOrder());

        List<Coupon> coupons = couponMapper.selectByExample(couponExample);


        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);
        long total = couponPageInfo.getTotal();

        ListVo listVo = new ListVo();
        listVo.setTotal(total);
        listVo.setItems(coupons);

        return listVo;
    }

    @Override
    public Coupon createCoupon(CouponBo couponBo) {
        StringBuffer goodsValue = new StringBuffer();

        for (String string : couponBo.getGoodsValue()) {
            goodsValue.append(string);
        }
        if (StringUtil.isEmpty(goodsValue.toString())) {
            goodsValue.append("[]");
        }

        Coupon coupon = new Coupon(
                couponBo.getId(),
                couponBo.getName(),
                couponBo.getDesc(),
                couponBo.getTag(),
                couponBo.getTotal(),
                couponBo.getDiscount(),
                couponBo.getMin(),
                couponBo.getLimit(),
                couponBo.getType(),
                couponBo.getStatus(),
                couponBo.getGoodsType(),
                goodsValue.toString(),
                couponBo.getCode(),
                couponBo.getTimeType(),
                couponBo.getDays(),
                couponBo.getStartTime(),
                couponBo.getEndTime(),
                couponBo.getAddTime(),
                couponBo.getUpdateTime(),
                couponBo.getDeleted());

        int insert = couponMapper.insert(coupon);

        Coupon newCoupon = couponMapper.selectByPrimaryKey(coupon.getId());

        return newCoupon;
    }

    @Override
    public ListVo getListuser(CouponUserListBo couponUserListBo) {
        PageHelper.startPage(couponUserListBo.getPage(), couponUserListBo.getLimit());

        CouponUserExample couponUserExample = new CouponUserExample();

        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        criteria.andCouponIdEqualTo(couponUserListBo.getCouponId());

        couponUserExample.setOrderByClause(couponUserListBo.getSort() + " " + couponUserListBo.getOrder());

        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);
        PageInfo<CouponUser> couponUserPageInfo = new PageInfo<>();
        long total = couponUserPageInfo.getTotal();


        ListVo listVo = new ListVo();
        listVo.setTotal(total);
        listVo.setItems(couponUsers);
        return listVo;
    }

    @Override
    public Coupon getCouponById(Integer id) {
        Coupon coupon = couponMapper.selectByPrimaryKey(id);
        return coupon;
    }

    @Override
    public Coupon updateCoupon(CouponBo updateCoupon) {
        StringBuffer goodsValue = new StringBuffer();
        for (String string : updateCoupon.getGoodsValue()) {
            goodsValue.append(string);
        }
        if (StringUtil.isEmpty(goodsValue.toString())) {
            goodsValue.append("[]");
        }

        Coupon coupon = new Coupon(
                updateCoupon.getId(),
                updateCoupon.getName(),
                updateCoupon.getDesc(),
                updateCoupon.getTag(),
                updateCoupon.getTotal(),
                updateCoupon.getDiscount(),
                updateCoupon.getMin(),
                updateCoupon.getLimit(),
                updateCoupon.getType(),
                updateCoupon.getStatus(),
                updateCoupon.getGoodsType(),
                goodsValue.toString(),
                updateCoupon.getCode(),
                updateCoupon.getTimeType(),
                updateCoupon.getDays(),
                updateCoupon.getStartTime(),
                updateCoupon.getEndTime(),
                updateCoupon.getAddTime(),
                updateCoupon.getUpdateTime(),
                updateCoupon.getDeleted());

        int i = couponMapper.insertSelective(coupon);

        return coupon;
    }

    @Transactional
    @Override
    public void deleteCoupon(Coupon updateCoupon) {
        int i = couponMapper.deleteByPrimaryKey(updateCoupon.getId());
        CouponUserExample couponUserExample = new CouponUserExample();

        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        CouponUserExample.Criteria criteria1 = criteria.andCouponIdEqualTo(updateCoupon.getId());
        int k = couponUserMapper.deleteByExample(couponUserExample);
    }

//    ---------------Topic相关------------

    @Override
    public ListVo getTopicList(TopicListBo topicListBo) {
        TopicExample topicExample = new TopicExample();

        PageHelper.startPage(topicListBo.getPage(), topicListBo.getLimit());

        TopicExample.Criteria criteria = topicExample.createCriteria();

        if (!StringUtil.isEmpty(topicListBo.getTitle())) {
            criteria.andTitleLike("%" + topicListBo.getTitle() + "%");
        }
        if (!StringUtil.isEmpty(topicListBo.getSubtitle())) {
            criteria.andTitleLike("%" + topicListBo.getSubtitle() + "%");
        }


        topicExample.setOrderByClause(topicListBo.getSort() + " " + topicListBo.getOrder());

        List<Topic> topics = topicMapper.selectByExample(topicExample);


        PageInfo<Topic> couponPageInfo = new PageInfo<>(topics);
        long total = couponPageInfo.getTotal();

        ListVo listVo = new ListVo();
        listVo.setTotal(total);
        listVo.setItems(topics);

        return listVo;
    }


    @Override
    public Topic createTopic(TopicCreatBo topicCreatBo) {
        StringBuffer value = new StringBuffer();
        for (String string : topicCreatBo.getGoods()) {
            value.append(string);
        }

        if (StringUtil.isEmpty(value.toString())) {
            value.append("[]");
        }
        Topic topic = new Topic(topicCreatBo.getTitle(),
                topicCreatBo.getSubtitle(),
                topicCreatBo.getPrice(),
                topicCreatBo.getReadCount(),
                topicCreatBo.getPicUrl(),
                value.toString(),
                topicCreatBo.getContent());

        int insert = topicMapper.insert(topic);

        Topic topic1 = topicMapper.selectByPrimaryKey(topic.getId());

        return topic1;
    }


    @Override
    public Topic updateTopic(Topic updateTopic) {
        int i = topicMapper.updateByPrimaryKey(updateTopic);

        Topic topic = topicMapper.selectByPrimaryKey(updateTopic.getId());

        return topic;
    }

    @Override
    public void deleteTopic(Topic updateTopic) {
        int i = topicMapper.deleteByPrimaryKey(updateTopic.getId());
        System.out.println(i);
    }


    //    -----------group相关-------------
    //实际表是GrouponRules
    @Override
    public ListVo getGrouponList(GroupListBo groupListBo) {
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();

        PageHelper.startPage(groupListBo.getPage(), groupListBo.getLimit());

        GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();


        if (groupListBo.getGoodsId() != null) {
            criteria.andGoodsIdEqualTo(groupListBo.getGoodsId());
        }


        grouponRulesExample.setOrderByClause(groupListBo.getSort() + " " + groupListBo.getOrder());

        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);


        PageInfo<GrouponRules> couponPageInfo = new PageInfo<>(grouponRules);
        long total = couponPageInfo.getTotal();

        ListVo listVo = new ListVo();
        listVo.setTotal(total);
        listVo.setItems(grouponRules);

        return listVo;
    }

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public GrouponRules createGroupon(GrouponRules createGrouponRules) throws ParseException {
        Integer goodsId = createGrouponRules.getGoodsId();
        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);

        createGrouponRules.setGoodsName(goods.getName());
        createGrouponRules.setPicUrl(goods.getPicUrl());
        int insert = grouponRulesMapper.insert(createGrouponRules);

        GrouponRules grouponRulesNew = grouponRulesMapper.selectByPrimaryKey(createGrouponRules.getId());

        return grouponRulesNew;
    }

    @Override
    public GrouponRules updateGrouponRules(GrouponRules updateGroupRules) {
        int i = grouponRulesMapper.updateByPrimaryKey(updateGroupRules);
        GrouponRules grouponRules = grouponRulesMapper.selectByPrimaryKey(updateGroupRules.getId());

        return grouponRules;
    }

    @Override
    public void deleteGroupon(GrouponRules deleteGrouponRules) {
        int i = grouponRulesMapper.deleteByPrimaryKey(deleteGrouponRules.getId());
    }


    @Override
    @Transactional
    public ListVo getGrouponListRecord(GrouponListRecordBo grouponListRecordBo) {

        PageHelper.startPage(grouponListRecordBo.getPage(), grouponListRecordBo.getLimit());
        List items = new ArrayList();
        GrouponListRecordVo grouponListRecordVo = new GrouponListRecordVo();


        //查询到goods 列表
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        if (grouponListRecordBo.getGoodsId() != null) {
            criteria.andIdEqualTo(grouponListRecordBo.getGoodsId());
        }

        List<Goods> goods = goodsMapper.selectByExample(goodsExample);

        for (Goods good : goods) {
            //先查询到所有 有该goodsId 的rulers记录
            Integer goodsId = good.getId();
            List<GrouponRules> grouponRulers = getGrouponRulers(goodsId);

            for (GrouponRules grouponRuler : grouponRulers) {

                Integer rulesId = grouponRuler.getId();
                Groupon groupon = getGropun(rulesId);

                GrouponListRecordVo one = new GrouponListRecordVo(groupon, good, grouponRuler);
                items.add(one);
            }


        }


        Integer size = items.size();
        long longSize = size.longValue();

        ListVo vo = new ListVo();
        vo.setItems(items);
        vo.setTotal(longSize);
        return vo;
    }


    private List<GrouponRules> getGrouponRulers(Integer goodsId) {
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);

        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);

        return grouponRules;
    }

    private Groupon getGropun(Integer rulesId) {
        return grouponMapper.selectByPrimaryKey(rulesId);

    }


}
